/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.assertj.ohos.api.components;

import ohos.agp.components.ComponentContainer;

import static org.assertj.ohos.internal.BitmaskUtils.buildNamedValueString;
import static org.assertj.core.api.Assertions.assertThat;

/** 组件容器断言 */
public abstract class ComponentContainerAssert<S extends ComponentContainerAssert<S, A>, A extends ComponentContainer>
    extends ComponentAssert<S, A> {
    protected ComponentContainerAssert(A actual, Class<S> selfType) {
    super(actual, selfType);
    }

    /**
    * 有子类个数
    *
    * @param count 数量
    * @return S
    */
    public S hasChildCount(int count) {
    isNotNull();
    int actualCount = actual.getChildCount();
    assertThat(actualCount) //
        .overridingErrorMessage("Expected child count <%s> but was <%s>", count, actualCount) //
        .isEqualTo(count);
    return myself;
    }

    /**
    * 后代聚焦
    *
    * @param focusability 聚焦
    * @return S
    */
    public S hasDescendantFocusability(int focusability) {
    isNotNull();
    int actualFocusability = actual.getFocusOrder();
    // noinspection ResourceType
    assertThat(actualFocusability) //
        .overridingErrorMessage("Expected descendant focusability <%s> but was <%s>",
            descendantFocusabilityToString(focusability),
            descendantFocusabilityToString(actualFocusability)) //
        .isEqualTo(focusability);
    return myself;
    }

    /**
    * 子代聚焦
    *
    * @param focusability 聚焦
    * @return String
    */
    public static String descendantFocusabilityToString(int focusability) {
    return buildNamedValueString(focusability)
        .value(ComponentContainer.FOCUS_CHILDREN_FIRST, "childrenFirst")
        .value(ComponentContainer.FOCUS_PARENT_FIRST, "parentFirst")
        .value(ComponentContainer.FOCUS_CHILDREN_HIDE, "childrenHide")
        .get();
    }
}
